<?php
namespace Modules\Groupon\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Groupon\Entities\GrouponRecord;

use Validator;
use ThrowException;
use Auth;

class GrouponRecordController extends Controller
{
    public function index(Request $req)
    {
        $rule = [
        ];

        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();
        $options = $req->all();

        $options['builder'] = $user->groupon_records();
        $options['with_count'] = ['members'];

        $options['only'] = [
            'id',
            'uuid',
            'union_status',
            'union_status_text',
            'need_member',
            'members_count',
            'end_at',
            'sku',
            'order',
            'created_at'
        ];

        $list = GrouponRecord::getList($options);

        $list->each(function($item) use ($user) {
            $item->order = $item->members()->where('user_id', $user->id)->first()->order;

            if ($item->order) {
                $orderSku = $item->order->sku;
                $sku = $orderSku->base_sku;

                $item->order = $item->order->getInfo([
                    'id',
                    'uuid'
                ]);

                if ($sku) {
                    $item->sku = array_merge($sku->getInfo([
                        'id',
                        'uuid',
                        'title',
                        'thumb',
                        'attrs',
                    ]), [
                        'total' => $orderSku->total,
                        'product' => $sku->product->getInfo(['id', 'uuid']),
                        'price' => $orderSku->price,
                        'selling_price' => $orderSku->selling_price,
                    ]);
                }
            }
        });

        return $list->generateListResponse();
    }

    public function show(Request $req, $uuid)
    {
        $record = GrouponRecord::getEntity([
            'uuid' => $uuid,
            'with' => ['members.base_user', 'product']
        ]);

        $info = $record->getInfo([
            'id',
            'uuid',
            'end_at',
            'union_status',
            'union_status_text',
            'need_member',
            'members_count',
            'members' => [
                '*' => [
                    'is_leader',
                    'base_user' => [
                        'name',
                        'headimg'
                    ]
                ]
            ],
            'product' => [
                'id',
                'uuid'
            ]
        ]);

        $order = $record->leader->order;

        $orderSku = $order->sku;
        $sku = $orderSku->base_sku;

        $info['sku'] = array_merge($sku->getInfo([
                        'id',
                        'uuid',
                        'title',
                        'thumb',
                        'attrs'
                    ]), [
                        'total' => $orderSku->total,
                        'product' => $sku->product->getInfo(['id', 'uuid']),
                        'price' => $orderSku->price,
                        'selling_price' => $orderSku->selling_price,
                    ]);


        $info['is_joined'] = 0;
        if (Auth::user() && $record->members()->paid()->where('user_id', Auth::user()->id)->first()) 
            $info['is_joined'] = 1;

        return [
            'info' => $info
        ];
    }
}
